package com.yalantis.ucrop.util;

import static android.os.Build.VERSION_CODES.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.luck.picture.lib.R;

import java.security.MessageDigest;


/**
 *  glide加载工具类
 * @author bullet
 * @date 2018/8/13
 */

@SuppressLint("CheckResult")
public class GlideUtil {

//    public static final int ERROR_PIC = com.luck.picture.lib.R.drawable.picture_icon_data_error;
    public static final int ERROR_PIC = com.luck.picture.lib.R.drawable.drawable_gray_latiduces;

    public static void putImg(Context context, Object bitmap, ImageView imageView) {
        Glide.with(context).load(bitmap).into(imageView);
    }




    /**
     * 加载圆形图片
     * @param url  图片地址
     * @param imageView ima
     */
    public static void  putRollImg( Object url, ImageView imageView) {
        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .placeholder(ERROR_PIC)
                .error(ERROR_PIC)
                .fallback(ERROR_PIC);

        Glide.with(imageView)
                .load(url)
//                .apply(options)
                .apply( new RequestOptions().centerCrop()
                        .placeholder(ERROR_PIC)
                        .error(ERROR_PIC)
                        .fallback(ERROR_PIC))
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imageView);
    }


    /**
     * 加载圆角图片
     * @param url
     * @param imageView
     * @param corner
     */
    public static void putrollCornerImg( Object url, ImageView imageView, int corner) {
        //设置图片圆角角度
        Glide.with(imageView)
                .load(url)
                .apply(new RequestOptions().centerCrop().placeholder(ERROR_PIC)
//                .error(R.mipmap.touxiang)
                .error(ERROR_PIC)
//                .fallback(R.mipmap.touxiang))
                .fallback(ERROR_PIC))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(corner)))
                .into(imageView);

    }


    /**
     *   加载一张正常的图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void putHttpImg(Context context, Object url, ImageView imageView) {
        Glide.with(context)
                .load(url)
//                .apply(new RequestOptions().centerCrop().placeholder(R.mipmap.touxiang)
                .apply(new RequestOptions().centerInside().placeholder(ERROR_PIC)//CENTER_INSIDE
//                .error(R.mipmap.touxiang)
                .error(ERROR_PIC)
//                .fallback(R.mipmap.touxiang))
                .fallback(ERROR_PIC))
//                .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                .into(imageView);
    }

    public static void putHttpImg(Object url, ImageView imageView) {
        Glide.with(imageView)
                .load(url)
//                .apply(new RequestOptions().centerCrop().placeholder(R.mipmap.touxiang)
                .apply(new RequestOptions().centerInside().placeholder(ERROR_PIC)//CENTER_INSIDE
//                .error(R.mipmap.touxiang)
                .error(ERROR_PIC)
//                .fallback(R.mipmap.touxiang))
                .fallback(ERROR_PIC))
//                .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                .into(imageView);
    }


    /**
     *  glide加载模糊图片
     * @param url
     * @param imageView
     */
    public static void putBlurImg(Object url, ImageView imageView) {
        Glide.with(imageView)
                .load(url)
//                .apply(new RequestOptions().centerCrop().placeholder(R.mipmap.touxiang)
//                .apply(new RequestOptions().centerInside().placeholder(ERROR_PIC)//CENTER_INSIDE
                .apply(RequestOptions.bitmapTransform(new GlideBlurTransformation(imageView.getContext()))
                        .error(ERROR_PIC)
                        .fallback(ERROR_PIC)
                )
                .into(imageView);
    }


    /**
     *   模糊图片的操作
     */
    public static class GlideBlurTransformation extends CenterCrop {
        private Context context;

        public GlideBlurTransformation(Context context) {
            this.context = context;
        }
        @Override
        protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap bitmap = super.transform(pool, toTransform, outWidth, outHeight);
            return BitmapUtil.blurBitmap(context, bitmap, 25, (int) (outWidth * 0.5), (int) (outHeight * 0.5));
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        }
    }

    /**
     * 获取 网络图片 Bitmap, 可能出现延迟
     * @param imgUrl 网络图片url
     */
    private static void requestUrlBitmap(Context context,String imgUrl, Action action) {
        Glide.with(context)
                .asBitmap()
                .load(imgUrl).into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        if (action!=null) action.invoke(resource);
                    }
                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    public interface Action{
        void invoke(Bitmap bitmap);
    }

}
